﻿

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using CodePlex.SpaceBlock.MSBuild;
using Microsoft.Build.BuildEngine;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Diagnostics;
using CodePlex.SpaceBlock;
using CodePlex.SpaceBlock.FS;

namespace CodePlex.SpaceBlock.Test
{
    /// <summary>
    ///This is a test class for CodePlex.SpaceBlock.MSBuild.S3Upload and is intended
    ///to contain all CodePlex.SpaceBlock.MSBuild.S3Upload Unit Tests
    ///</summary>
    [TestClass()]
    public class MSBuildTests
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }


        /// <summary>
        ///A test for S3Upload ()
        ///</summary>
        [TestMethod]
        public void Test_MSBuild()
        {

            ITaskRunner runner = new MSBuildTaskRunner(this.TestContext);


            StaticTests.SharedTests(runner);

        }


        private class MSBuildTaskRunner : ITaskRunner
        {
            private readonly TestContext _context;
            public MSBuildTaskRunner(TestContext context)
            {
                _context = context;
            }

            public bool RunTask(string taskName, params NameValue[] parameters)
            {
                Engine engine = Engine.GlobalEngine;


                engine.BinPath = @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\";

                engine.UnregisterAllLoggers();
                engine.RegisterLogger(new TestContextLogger(_context));

                Project project = engine.CreateNewProject();



                project.AddNewUsingTaskFromAssemblyName(taskName, "CodePlex.SpaceBlock.MSBuild");

                Target target = project.Targets.AddNewTarget("MockTarget");

                BuildTask task = target.AddNewTask(taskName);


                foreach (NameValue parameter in parameters)
                {
                    task.SetParameterValue(parameter.Name, parameter.Value);
                }



               return engine.BuildProject(project);

            }




            private class TestContextLogger : Logger
            {
                private readonly TestContext _context;
                public TestContextLogger(TestContext context)
                {
                    _context = context;
                }
                public override void Initialize(IEventSource eventSource)
                {
                    eventSource.AnyEventRaised += delegate(object sender, BuildEventArgs e)
                    {
                        _context.WriteLine("{0}", e.Message);
                    };
                }


            }

           
        }


    }

}
